package com.gather.spring.retry.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author kxd
 * @date 2018/12/5 10:27
 * description:
 */
@Aspect
@Component
public class RetryAspect {

    //切面中参数设置需要理解有问题
    @Pointcut("execution(public *com.javause.useretry*)&&" +
            "@annotation(useretry.Retryable)")
    public void myPointcut() {

    }

    @Around("myPointcut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        Method method = getCurrentMethod(point);
        Retryable retryable = method.getAnnotation(Retryable.class);


        //1最大次数判断
        int maxAttempts = retryable.maxAttempts();
        if (maxAttempts <= 1) {
            return point.proceed();
        }
        //异常处理

        int times = 0;
        final Class<? extends Throwable> exceptionClass = retryable.value();
        while (times < maxAttempts) {
            try {

            } catch (Throwable e) {
                times++;
                if (times >= maxAttempts || !e.getClass().isAssignableFrom(exceptionClass)) {
                    throw new Throwable(e);
                }
            }
        }
        return null;
    }

    private Method getCurrentMethod(ProceedingJoinPoint point) {
      /*  try {
            Signature sig = point.getSignature();
            MapperMethod.MethodSignature msig = (MethodSignature) sig;
            Object target = point.getTarget();
            return target.getClass().getMethod(msig.getName(), msig.getParameterTypes());
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }*/
      return null;
    }
}
